package leets.leet2349;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;

public class Solution {
}
class NumberContainers {
    Map<Integer,Integer> map=new HashMap<>();
    Map<Integer, TreeSet<Integer>> map1;
    public NumberContainers() {

        map1=new HashMap<>();
    }

    public void change(int index, int number) {
        if (map.containsKey(index)){
            int oldNumber=map.get(index);
            map1.get(oldNumber).remove(index);
        }
        map1.computeIfAbsent(number,k-> new TreeSet<>(Comparator.comparingInt(o -> o))).add(index);
        map.put(index,number);

    }

    public int find(int number) {
        if (map1.containsKey(number)&&!map1.get(number).isEmpty()){
            return map1.get(number).first();
        }else {
            return -1;
        }
    }
}
